package bg.btanev.eightyfour.domain.experience;

import org.apache.commons.lang.Validate;

import bg.btanev.eightyfour.domain.experience.institution.WorkInstitution;
import bg.btanev.eightyfour.domain.person.Person;

/**
 *
 * @author B.Tanev
 *
 */
public class WorkExperience extends Experience<WorkInstitution> {
	/**
	 *
	 */
	private static final long serialVersionUID = 5084866244461240371L;

	private String position;

	protected WorkExperience() {
		super();
	}

	public WorkExperience(ExperiencePeriod period, WorkInstitution institution, Person person, String position) {
		this(null, period, institution, person, position);
	}

	public WorkExperience(Long id, ExperiencePeriod period, WorkInstitution institution, Person person, String position) {
		super(id, period, institution, person);

		Validate.notEmpty(position, "Position cannot be empty.");
		this.position = position;
	}

	public String getPosition() {
		return position;
	}

	@Override
	protected Person updatePersonInternal(Person person) {
		Person oldPerson = getPerson();
		if (oldPerson != null) {
			oldPerson.removeWorkExperience(this);
		}
		person.addWorkExperience(this);

		return person;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((position == null) ? 0 : position.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		WorkExperience other = (WorkExperience) obj;
		if (position == null) {
			if (other.position != null)
				return false;
		} else if (!position.equals(other.position))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "WorkExperience [position=" + position + ", toString()=" + super.toString() + "]";
	}
}
